﻿using System;
using System.Windows.Forms;
using Quakk.Twitter;

namespace Quakk.Controls
{
    public class MenuPanel : Panel
    {
        private bool isPaintBackgroundComplete = true;
        private readonly Tweet tweet;

        public event EventHandler<MenuEventArgs> Reply;
        public event EventHandler<MenuEventArgs> OpenLink;
        public event EventHandler<MenuEventArgs> SendDirect;
        public event EventHandler<MenuEventArgs> ViewPicture;

        public MenuPanel(Tweet tweet)
        {
            this.tweet = tweet;

            AddButtons();
            RearrangeButtons();
        }

        private void AddButtons()
        {
            MenuButton button = new MenuButton
                                    {
                                        Text = "Send Reply",
                                    };
            button.Click += OnReplyClick;
            Controls.Add(button);

            MenuButton directButton = new MenuButton
            {
                Text = "Send Direct Msg",
            };
            directButton.Click += OnDirectClick;
            Controls.Add(directButton);

            // If the tweet contains a Url then provide an Open Url or View Picture button
            if (tweet.Message.ToLower().IndexOf("http://") > -1 || tweet.Message.ToLower().IndexOf("www.") > -1)
            {
                if (tweet.Message.ToLower().IndexOf("http://twitpic.com") > -1)
                {
                    button = new MenuButton
                    {
                        Text = "View Picture"
                    };
                    button.Click += OnViewPictureClick;
                    Controls.Add(button);
                }
                else
                {
                    button = new MenuButton
                                 {
                                     Text = "Open Link"
                                 };
                    button.Click += OnOpenLinkClick;
                    Controls.Add(button);
                }
            }
        }

        private void OnViewPictureClick(object sender, EventArgs e)
        {
            if (ViewPicture != null)
            {
                ViewPicture(this, new MenuEventArgs { Tweet = tweet });
            }
        }

        private void RearrangeButtons()
        {
            const int margin = 40;
            int buttonCount = Controls.Count;

            if(true)
            {
                int totalHeight = Height - (margin*2);
                int buttonWidth = Width - (margin*2);
                int buttonHeight = totalHeight/buttonCount;
                if(buttonHeight > 100)
                {
                    buttonHeight = 100;
                }

                int buttonLeft = margin;
                int buttonTop = (Height - (buttonCount*buttonHeight))/2;

                foreach (Control control in Controls)
                {
                    control.Left = buttonLeft;
                    control.Top = buttonTop;
                    control.Width = buttonWidth;
                    control.Height = buttonHeight;
                    buttonTop += buttonHeight;
                }
            }
        }

        protected void OnReplyClick(object sender, EventArgs e)
        {
            if (Reply != null)
            {
                Reply(this, new MenuEventArgs {Tweet = tweet});
            }
        }

        protected void OnDirectClick(object sender, EventArgs e)
        {
            if (SendDirect != null)
            {
                SendDirect(this, new MenuEventArgs { Tweet = tweet });
            }
        }

        protected void OnOpenLinkClick(object sender, EventArgs e)
        {
            if (OpenLink != null)
            {
                OpenLink(this, new MenuEventArgs { Tweet = tweet });
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            Dispose();
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            RearrangeButtons();
        }

        // Overridden for some reason, but can't remember why
        protected override void OnPaintBackground(PaintEventArgs e)
        {
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (!isPaintBackgroundComplete)
            {
                Visible = false;
                Parent.Invalidate(Bounds);
                Parent.Update();
                Visible = true;
                isPaintBackgroundComplete = true;

                return;
            }
            isPaintBackgroundComplete = false;
        }
    }

    public class MenuEventArgs : EventArgs
    {
        public Tweet Tweet
        {
            get; set;
        }
    }
}
